package soar.template.springboot.syncbuffer.configuration.rabbitmq;

import javax.annotation.Resource;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import soar.template.springboot.syncbuffer.appconfig.AppConfig;
import soar.template.springboot.syncbuffer.appconfig.rabbitmq.Config;
import soar.template.springboot.syncbuffer.rabbitmq.MessageConsumer;


@Configuration
class MyRabbitMQConfiguration
{
    @Resource(name = "appconfig")
    private AppConfig appconfig;

    @Bean
    public ConnectionFactory mainConnectionFactory()
    {
        Config config = appconfig.rabbitmq.main;
        com.rabbitmq.client.ConnectionFactory orig_factory = new com.rabbitmq.client.ConnectionFactory();
        orig_factory.setHost(config.host);
        orig_factory.setPort(config.port);
        orig_factory.setVirtualHost(config.vhost);
        orig_factory.setUsername(config.username);
        orig_factory.setPassword(config.password);
        ConnectionFactory factory = new CachingConnectionFactory(orig_factory);
        return factory;
    }

    /*--- 监听接收 ---*/
    @Bean
    public DirectExchange mainDirectExchange()
    {
        return new DirectExchange(appconfig.rabbitmq.main.exchange, true,
                false);
    }

    @Bean
    public Queue mainQueue()
    {
        return new Queue(appconfig.rabbitmq.main.queue, true, false,
                false);
    }

    @Bean
    public Binding mainBinding()
    {
        return BindingBuilder.bind(mainQueue()).to(mainDirectExchange())
                .with(appconfig.rabbitmq.main.queue);
    }

    /*--- 发送 ---*/
    @Bean(name = "mainRabbitTemplate")
    public RabbitTemplate mainRabbitTemplate()
    {
        RabbitTemplate template = new RabbitTemplate();
        template.setConnectionFactory(mainConnectionFactory());
        return template;
    }

    @Resource
    private MessageConsumer messageConsumer;

    @Bean
    public MessageListenerAdapter mainMessageListenerAdapter()
    {
        return new MessageListenerAdapter(messageConsumer, "handle");
    }

    @Bean
    public SimpleMessageListenerContainer mainSimpleMessageListenerContainer()
    {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(mainConnectionFactory());
        container.setQueueNames(appconfig.rabbitmq.main.queue);
        container.setMessageListener(mainMessageListenerAdapter());
        return container;
    }

}
